/*
  Tilt
  tilt.cpp

  Copyright (C) Rui Barreiros

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

  This plugin uses an algorithm from Lubomir I. Ivanov, all that was done
  by me was to implement this algorithm in a LV2 plugin.

 */

#include <lv2plugin.hpp>
#include <math.h>
#include "tilt.peg"
#include "inline.h"

using namespace LV2;

class Tilt : public Plugin<Tilt>
{
 public:
  Tilt(double rate) : 
    Plugin<Tilt>(p_n_ports), sRate(rate), 
    lp_out_L(0), lp_out_R(0), lgain(0), hgain(0), a0(0), b1(0)
  {
    amp = 6/log(2);
    sr3 = 3 * sRate;
  }

  void run(uint32_t nframes)
  {
    if(*p(p_bypass) > 0)
    {
      return;
    }

    float *insliceL = p(p_in_L);
    float *insliceR = p(p_in_R);
    float *outsliceL = p(p_out_L);
    float *outsliceR = p(p_out_R);

    calcGain(amp, *p(p_gain), &lgain, &hgain);
    calcFilter(*p(p_cfrequency), sr3, &a0, &b1);

    // sample loop
    for(int i = 0; i < nframes; i++)
    {
      calcCoeff(insliceL[i], a0, b1, &lp_out_L);
      calcCoeff(insliceR[i], a0, b1, &lp_out_R);

      applyCoeff(insliceL[i], lgain, hgain, lp_out_L, &outsliceL[i]);
      applyCoeff(insliceR[i], lgain, hgain, lp_out_R, &outsliceR[i]);
    }
  }

 private:
  double sRate, lp_out_L, lp_out_R, amp, sr3, lgain, hgain, a0, b1;

};

static int _ = Tilt::register_class("http://www.ngen.org/rbarreiros/plugins/tilt");
